#include "state_info.h"

StateInfo::StateInfo(int64_t id, int64_t parent_id, QString state_name, QString description, QString host, int port)
    : id_(id),
      parent_id_(parent_id),
      state_name_(state_name),
      description_(description),
      host_(host),
      port_(port),
      is_deleted_(false)
{
}

StateInfo::~StateInfo()
{
}

int StateInfo::appendASDU(int asdu_addr, QString device_name, int type_i)
{
    ASDUInfo asdu_info;
    asdu_info.asdu_addr = asdu_addr;
    asdu_info.device_name = device_name;
    asdu_info.asdu_type = static_cast<ASDUInfo::Type>(type_i);
    asdu_vec_.push_back(asdu_info);
    return asdu_vec_.size() - 1;
}

QString StateInfo::stateName() const
{
    return state_name_;
}

QString StateInfo::description() const
{
    return description_;
}

QString StateInfo::host() const
{
    return host_;
}

ASDUInfo StateInfo::asdu(int index) const
{
    if (index >= 0 && index < static_cast<int>(asdu_vec_.size()))
    {
        return asdu_vec_[index];
    }
    return ASDUInfo();
}

void StateInfo::setStateName(const QString &name)
{
    this->state_name_ = name;
}

void StateInfo::setDescription(const QString &desc)
{
    this->description_ = desc;
}

void StateInfo::setHost(const QString &host)
{
    this->host_ = host;
}

void StateInfo::setPort(int port)
{
    this->port_ = port;
}

int StateInfo::port() const
{
    return port_;
}

int64_t StateInfo::id() const
{
    return id_;
}

void StateInfo::setId(int64_t id)
{
    this->id_ = id;
}

int64_t StateInfo::parentId() const
{
    return parent_id_;
}

void StateInfo::setParentId(int64_t parent_id)
{
    this->parent_id_ = parent_id;
}

void StateInfo::setIsRtu(bool is_rtu)
{
    this->is_rtu_ = is_rtu;
}

bool StateInfo::isRtu() const
{
    return is_rtu_;
}

std::vector<PointInfo> StateInfo::points() const
{
    return point_vec_;
}

void StateInfo::setPointVec(const std::vector<PointInfo> &point_vec)
{
    this->point_vec_ = point_vec;
}

void StateInfo::setDeletedFlag(bool is_deleted)
{
    this->is_deleted_ = is_deleted;
}

bool StateInfo::isDeleted() const
{
    return is_deleted_;
}

QString StateInfo::toJson() const
{
    return QString("{\"id\":%1, \"pid\":%2, \"host\":\"%3\",\"port\":%4,\"description\":\"%5\",\"isRtu\":%6}")
        .arg(id_)
        .arg(parent_id_)
        .arg(host_)
        .arg(port_)
        .arg(state_name_)
        .arg(is_rtu_ ? "true" : "false");
}

StateInfo::PointSetting StateInfo::pointSetting() const
{
    return point_setting_;
}

void StateInfo::setPointSetting(const PointSetting &point_setting)
{
    this->point_setting_ = point_setting;
}